package com.peter.dialog_custom;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private Context mContext = this;

    private SelfDialog selfDialog;

    private static final String TAG = MainActivity.class.getName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                selfDialog = new SelfDialog(MainActivity.this);
                selfDialog.setTitle("提示");
                selfDialog.setMessage("确定退出应用?");
                selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        Toast.makeText(MainActivity.this, "点击了--确定--按钮", Toast.LENGTH_LONG).show();
                        selfDialog.dismiss();
                    }
                });
                selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        Toast.makeText(MainActivity.this, "点击了--取消--按钮", Toast.LENGTH_LONG).show();
                        selfDialog.dismiss();
                    }
                });

                selfDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        Log.w(TAG, "onDismiss: ", null);
                    }
                });

                selfDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        Log.w(TAG, "onCancel: ", null);
                    }
                });

                selfDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialogInterface) {
                        showSoftInput(Objects.requireNonNull(selfDialog.findViewById(R.id.dialog_input_area)));
                    }
                });

                selfDialog.show();
            }
        });

    }

    private void showSoftInput(final View view) {
        view.postDelayed(() -> {
            InputMethodManager mImm = (InputMethodManager) view.getContext().getApplicationContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            view.requestFocus();
            mImm.showSoftInput(view, 0);
        }, 100);
    }
}